package com.example.demo.controller.admin;

import com.example.demo.Util.Result;
import com.example.demo.Util.ResultUtil;
import com.example.demo.Util.UpPhotoNameUtils;
import com.example.demo.mapper.UserMap;
import com.example.demo.po.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

@RestController
@RequestMapping("/file")
public class FileController {
    @Autowired
    UpPhotoNameUtils upPhotoNameUtils;

    @Autowired
    UserMap userMap;

    @PostMapping("/userimage")
    public Result ChangeUserPic(@RequestParam("file") MultipartFile file, @RequestParam("id") int id) throws Exception{

        String userPic = upPhotoNameUtils.SaveUserPic("UserPic", file);
        if(userPic==null){
            return ResultUtil.fail("null") ;
        }
         userMap.UpdateUserAvatar(id,userPic);
        System.out.println(userPic);
        return ResultUtil.success(userPic) ;
    }

    @PostMapping("/materialimage")
    public Result ChangeMaterialPic(@RequestParam("file") MultipartFile file, @RequestParam("id") int id) throws Exception{

        String userPic = upPhotoNameUtils.SaveUserPic("UserPic", file);
        if(userPic==null){
            return ResultUtil.fail("null") ;
        }



        //user.setUserPic(userPic);
        //  boolean flag = userService.UpdataUser(user);
        //   if(flag){
        //      spaceMessage.setUserPic(user.getUserPic());
        //     spaceMessage.setSuccess(1);
        // }

        // else
        //    spaceMessage.setSuccess(0);

        return ResultUtil.success(userPic) ;
    }


}
